<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 网商银行支付宝H5
 */
class Wangshang10Controller extends PayController{
    protected $config;

    public function pay($array){
        //$this->config = M("Channel")->where(array('id'=>$array['channel']))->find();
        $this->config = Channel::getInstance()->getById($array['channel']);
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>'012']);
        }
		
        //$product = M("Product")->where(["id"=> I("request.pay_bankcode","")])->find();
        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>'012']);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $data = array();
        $data['payType'] = 'syt';
        $data['partner'] = $this->config['mch_id'];
        $data['orderId'] = $return['orderid'];
        $data['orderAmount'] = $return['amount'];
        $data['version'] = '1.0';
        $data['signType'] = 'MD5';

        switch($this->config['paytype']){
            //支付宝H5
            case 4:
                $data['payMethod'] = 22;
                break;
            //微信
            case 1:
                $data['payMethod'] = 11;
                break;
            //支付宝
            case 3:
                $data['payMethod'] = 22;
                break;
            //qq
            case 8:
                $data['payMethod'] = 33;
                break;
            //京东
            case 9:
                $data['payMethod'] = 55;
                break;
            default:
                $this->showmessage('不支持此产品!',['code'=>'012']);
        }

        ksort($data);
        $str = http_build_query($data).$this->config['signkey'];
        $data['sign'] = strtoupper(md5($str));
        $data['productName'] = 'zf';

        $data['notifyUrl'] = $return['notifyurl'];

        $apiurl = $this->config['gateway'];

        po($apiurl, $data);
    }

    //通知
    public function notifyurl(){
        if(!$this->isWangshangIp()){
            exit("");
        }
        $p = file_get_contents('php://input');

        $sign = substr($p,0, stripos($p,"|"));
        $json = substr($p,stripos($p,"|")+1);

        $arr = json_decode($json, true);

        //获取订单信息
        $orderid = $arr['data']['orderId'];
        $find = M("Order")->where(array("pay_orderid"=>$orderid))->find();
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(
            "server_return"=> file_get_contents('php://input'),
            "pay_orderid_rd"=>$arr['data']['trade_no']
        ));
        Order::getInstance()->edit($find['id'],array("server_return"=>file_get_contents('php://input'), "pay_orderid_rd"=>$arr['data']['trade_no']));
        Ordermember::getInstance(getIdByBianHao($find['pay_memberid']))->edit($find['id'],array("server_return"=>file_get_contents('php://input'), "pay_orderid_rd"=>$arr['data']['trade_no']));

        //获取渠道信息
        $this->config = Channel::getInstance()->find(['code'=>'Wangshang10']);
        if(!$this->config){
            $this->config = M("Channel")->where(array('code'=>'Wangshang10'))->find();
        }

        if(!$this->config){
            exit("get_tongdao_fail");
        }

        if(!$arr || empty($arr)){
            exit("wrong");
        }

        if($arr['code'] != '000000' && $arr['message'] != 'PaySuccess'){
            exit("pay_fail");
        }

        $str1 = md5(base64_encode($json));
        $str2 = $this->config['signkey']. $str1;
        $str3 = md5($str2);

        $sign2 = strtoupper($str3);

        if($sign != $sign2){
            echo 'sign_wrong';exit;
        }

        //处理支付
        $this->EditMoney($find['pay_orderid'],"Wangshang10",0);

        //回调成功
        echo 'success'; exit;
    }

    public function callBackUrl(){
        echo '处理成功'; exit;
    }
}